<template>
  <div class="siteMap mt-80">
    <routestationAll
      :routeName="routeName"
      :collectDatas="collectDatas"
      :isBackBtn="true"
    />
  </div>
</template>

<script>
import routestationAll from "@/components/routestationAll";
import { addHistory, getStationRoute, getPredict } from "@/api/collect";
import { getRefreshTime } from "@/utils/location";
import { mapState } from "vuex";

export default {
  components: {
    routestationAll,
  },
  data() {
    return {
      refreshTime: null,
      collectDatas: [],
      getPredictList: [],
    };
  },
  computed: {
    ...mapState(["lnglat", "changeRefresh"]),
  },
  created() {
    this.routeName = this.$route.params.routeName;
    this.refreshTime = getRefreshTime(this.$store.state.changeRefresh); //刷新设置时间
    this.addHistory();
    // 获取选中站点的所有线路
    if (this.$route.params.index == 0) {
      this.getStationRoute();
    } else {
      this.collectDatas.push(this.$store.state.routeListStore);
      console.log('获取选中站点的所有线路',this.collectDatas);
    }
  },
  watch: {
    changeRefresh() {
      this.refreshTime = getRefreshTime(this.$store.state.changeRefresh);
    },
  },

  methods: {
    async getPredict() {
      let params = {
        stationId: this.$route.params.stationid,
        segmentid: this.$route.params.segmentid,
      };
      let { status, data } = await getPredict(params);
      if (status == 200) {
        return data;
      }
    },
    async getStationRoute() {
      clearInterval(this.setTime);
      let params = {
        stationId: this.$route.params.stationid,
        userId: localStorage.getItem("userId")
          ? localStorage.getItem("userId")
          : "", //用户id
      };
      let { status, data } = await getStationRoute(params);
      if (status == 200) {
        this.getPredict().then((res) => {
          this.getPredictList = res;
          this.collectDatas = data;
          this.collectDatas.forEach((item) => {
            this.getPredictList.forEach((i) => {
              if (item.routeid == i.routeId) {
                item.firstBusState = i.firstBusState; //第一辆车状态  1正常预测；2不能预测；3无车可预测
                item.firstBusStationSum = i.firstBusStationSum; //第一辆车距当前站数
                item.firstBusPtime = i.firstBusPtime; //第一辆预测时间
                item.secondBusState = i.secondBusState; //第一辆车状态  1正常预测；2不能预测；3无车可预测
                item.secondBusStationSum = i.secondBusStationSum; //第一辆车距当前站数
                item.secondBusPtime = i.secondBusPtime; //第一辆预测时间

                item.ifHavePredictBus = i.ifHavePredictBus; //是否有预测车辆
                item.nextSendTime = i.nextSendTime; //下一辆车发车时间

                item.predictState = i.predictState; //预测总体状态
                item.firstBusDesc = i.firstBusDesc; //预测总体情况描述
              }
            });
          });
        });
      }
      this.setTime = setInterval(async () => {
        let { status, data } = await getStationRoute(params);
        if (status == 200) {
          this.getPredict().then((res) => {
            this.getPredictList = res;
            this.collectDatas = data;
            this.collectDatas.forEach((item) => {
              this.getPredictList.forEach((i) => {
                if (item.routeid == i.routeId) {
                  item.firstBusState = i.firstBusState; //第一辆车状态  1正常预测；2不能预测；3无车可预测
                  item.firstBusStationSum = i.firstBusStationSum; //第一辆车距当前站数
                  item.firstBusPtime = i.firstBusPtime; //第一辆预测时间
                  item.secondBusState = i.secondBusState; //第二辆车状态  1正常预测；2不能预测；3无车可预测
                  item.secondBusStationSum = i.secondBusStationSum; //第二辆车距当前站数
                  item.secondBusPtime = i.secondBusPtime; //第二辆预测时间

                  item.ifHavePredictBus = i.ifHavePredictBus; //是否有预测车辆
                  item.nextSendTime = i.nextSendTime; //下一辆车发车时间

                  item.predictState = i.predictState; //预测总体状态
                  item.firstBusDesc = i.firstBusDesc; //预测总体情况描述
                }
              });
            });
          });
        }
      }, this.refreshTime);
    },

    async addHistory() {
      if (!localStorage.getItem("userId")) return;
      let currentData = this.$store.state.routeListStore;
      let params = {
        userid: localStorage.getItem("userId"),
        routeid: currentData.routeid,
        rundirection: currentData.rundirection,
        routename: currentData.routename,
      };
      await addHistory(params);
    },
  },
  destroyed() {
    clearInterval(this.setTime);
  },
};
</script>
<style lang="scss" scoped>
</style>
